#!/usr/bin/env python

import sys, getopt, switch

def usage():
	print "usage: change_vlan [-h] -a address --port port --vlan number"
	print "\t-a or --address\t specify the IP address of the switch"
	print "\t-h\t\t display this help"
	print "\t--port\t\t the port number on the switch"
	print "\t--vlan\t\t the vlan number"
	print "\n\texample: ./change_vlan.py -a 192.168.254.1 --port 1 --vlan 100"
	print

def run_change_vlan(ip, port, vlan):
	print "Connecting to " + ip + "..."
	tn = switch.connect(ip)
	if not tn:
		print "Error while connecting to " + ip
		return
	name = switch.enable(tn)
	if not name:
		print "Error while connecting to " + ip
		return
	print "Connected on " + name
	print "Changing the port " + port + " to the VLAN " + vlan
	res = switch.change_vlan(tn, port, vlan)
	switch.quit(tn)
	if not res:
		print "Error"
		sys.exit(1)
	else:
		print "Done"
	print

def main(argv):
	try:
		opts, args = getopt.getopt(argv[1:], "a:h", ["help", "address=", "port=", "vlan="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
	address = ""
	port = ""
	vlan = ""
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit()
		elif opt in ("-a", "--address"):
			address = arg
		elif opt == "--port":
			port = arg
		elif opt in "--vlan":
			vlan = arg
	if address == "" or port == "" or vlan == "":
		usage()
		sys.exit(2)
	run_change_vlan(address, port, vlan)

if __name__ == "__main__":
	main(sys.argv)
